package com.soulkun.smarthome.ui.main.personal

import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.navGraphViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainPersonalFamilySettingRoomBinding
import com.soulkun.smarthome.databinding.AppRecycleItemMainPersonalFamilyRoomRoomListBinding
import com.soulkun.smarthome.ui.main.personal.adapter.AppMainPersonalFamilyRoomRoomListAdapter
import com.soulkun.smarthome.ui.main.personal.dialog.AppMainPersonalFamilyRoomAddDialog
import com.soulkun.smarthome.ui.main.personal.dialog.AppMainPersonalFamilyRoomDeleteDialog
import com.soulkun.smarthome.ui.main.personal.dialog.AppMainPersonalFamilyRoomRenameDialog
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilySettingRoomViewModel
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilySettingSharedViewModel
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilySettingViewModel
import soulkun.library.mvvm.adapter.clearAndAddAll
import soulkun.library.mvvm.component.BaseDataBindingFragment
import soulkun.library.utils.screen.ScreenUtils

class AppMainPersonalFamilySettingRoomFragment :
    BaseDataBindingFragment<AppFragmentMainPersonalFamilySettingRoomBinding>() {

    private val sharedViewModel by navGraphViewModels<AppMainPersonalFamilySettingSharedViewModel>(R.id.app_navigation_main_personal_family_setting)
    private val viewModel by viewModels<AppMainPersonalFamilySettingRoomViewModel> { defaultViewModelProviderFactory }

    private val renameDialog by lazy {
        AppMainPersonalFamilyRoomRenameDialog()
    }
    private val deleteDialog by lazy {
        AppMainPersonalFamilyRoomDeleteDialog()
    }
    private val addDialog by lazy {
        AppMainPersonalFamilyRoomAddDialog()
    }

    private val adapter by lazy {
        AppMainPersonalFamilyRoomRoomListAdapter(
            mContext,
            viewModel.familyRoomList,
            viewModel.managerStatus
        ) { binding ->
            View.OnClickListener {
                if ((binding as AppRecycleItemMainPersonalFamilyRoomRoomListBinding).managerStatus!!.get()) {
                    viewModel.selectFamilyRoomLiveData.value = binding.roomVO!!
                    deleteDialog.show(childFragmentManager, "DeleteDialog")
                } else {
                    viewModel.selectFamilyRoomLiveData.value = binding.roomVO!!
                    renameDialog.show(childFragmentManager, "RenameDialog")
                }
            }
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_personal_family_setting_room
    }

    override fun initView() {
        ScreenUtils.setViewFitSystemWindows(requireDataBinding().root)
        requireDataBinding().rvMainPersonalFamilySettingRoomListRoom.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainPersonalFamilySettingRoomFragment.adapter
        }
    }

    override fun initData() {
        viewModel.familyId = sharedViewModel.familyId
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.iv_main_personal_family_setting_room_icon_back -> {
                    findNavController().navigateUp()
                }
                R.id.iv_main_personal_family_setting_room_icon_manager -> {
                    viewModel.managerStatus.set(true)
                }
                R.id.tv_main_personal_family_setting_room_text_manager -> {
                    viewModel.managerStatus.set(false)
                }
                R.id.tv_main_personal_family_setting_room_text_add_room -> {
                    addDialog.show(childFragmentManager, "AddDialog")
                }
            }
        }
        viewModel.familyRoomList.clearAndAddAll(sharedViewModel.familyRoomList)
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

}